package com.cyz.springdemo.Controller.meituan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cyz.springdemo.model.BaseResponse;
import com.sankuai.meituan.waimai.opensdk.exception.ApiOpException;
import com.sankuai.meituan.waimai.opensdk.exception.ApiSysException;
import com.sankuai.meituan.waimai.opensdk.factory.APIFactory;
import com.sankuai.meituan.waimai.opensdk.vo.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: springdemo
 * @description: 美团外卖Api
 * @author: wangjie
 * @create: 2019-09-26 09:26
 **/
@RestController
@RequestMapping(value = "/meituan/Test")
public class TestController {

    //appId:商家的ID;appSecret:商家签名
    private final static SystemParam sysPram = new SystemParam("2613","866279a25ae18a9a0ebeb8ab89aa2677");
    //APP方的ID
    private final static String appPoiCode = "t_5AJtogG08y";

    @RequestMapping(value = "/poiSave")
    public void poiSave() {
        PoiParam PoiPram = new PoiParam();
        PoiPram.setApp_poi_code("ceshi_poi1");
        PoiPram.setName("我的门店");
        PoiPram.setAddress("我的门店的地址");
        PoiPram.setLatitude(40.810249);
        PoiPram.setLongitude(117.502289);
        PoiPram.setPhone("13425355733");
        PoiPram.setShipping_fee(2f);
        PoiPram.setShipping_time("09:00-13:30,19:00-21:40");
        PoiPram.setPic_url("http://cdwuf.img46.wal8.com/img46/525101_20150811114016/144299728957.jpg");
        PoiPram.setOpen_level(1);
        PoiPram.setIs_online(1);
        PoiPram.setPre_book_min_days(1);
        PoiPram.setPre_book_max_days(2);
        PoiPram.setApp_brand_code("zhajisong");
        PoiPram.setThird_tag_name("麻辣烫");

        try {
            String result = APIFactory.getPoiAPI().poiSave(sysPram, PoiPram);
            System.out.println(result);
        } catch (ApiOpException e) {
            e.printStackTrace();
        } catch (ApiSysException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取商户ID
     */
    @RequestMapping(value = "/poiGetIds", method = RequestMethod.GET)
    public void poiGetIds() {
        try {
            String result = APIFactory.getPoiAPI().poiGetIds(sysPram);
            System.out.println(result);
        } catch (ApiOpException e) {
            e.printStackTrace();
        } catch (ApiSysException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取商户
     * @return
     */
    @RequestMapping(value = "/poiTagList", method = RequestMethod.GET)
    public Object poiTagList(){
        BaseResponse response = new BaseResponse();
        try{
            List<PoiTagParam> list = APIFactory.getPoiAPI().poiTagList(sysPram);
            response.setData(list);
        }catch (ApiSysException e) {
            e.printStackTrace();
        } catch (ApiOpException e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 查询商品详情
     * @param appPoiCodeParam 商户第三方ID
     * @param appFoodCode APP方商品ID(SPU_code)
     * @return
     */
    @RequestMapping(value = "/poiRetailGet")
    public Object poiRetailGet(String appFoodCode,String appPoiCodeParam){
        BaseResponse response = new BaseResponse();
        try{
            FoodParam foodParam = APIFactory.getRetailApi().retailGet(sysPram,appPoiCodeParam,appFoodCode);
            response.setData(foodParam);
        } catch (ApiSysException e) {
            e.printStackTrace();
            response.setMessage(e.getMessage());
        } catch (ApiOpException e) {
            response.setCode(e.getCode());
            response.setMessage(e.getMsg());
        }
        return response;
    }

    /**
     * 根据UPC码批量新增/更新商品
     * @param appPoiCodeParam   商户ID
     * @param retailParams      批量商品对象集合
     * @return
     */
    @RequestMapping(value = "/retailBatchInitDataByUpc",method = RequestMethod.POST)
    public Object retailBatchInitDataByUpc(String appPoiCodeParam,
                                           @RequestBody List<RetailParam> retailParams){
        BaseResponse response = new BaseResponse();
        try {
            //调用新版的RetailApi
            String result = APIFactory.getNewRetailApi().retailBatchInitDataByUpc(sysPram,appPoiCodeParam,retailParams);
            JSONObject resultJson = JSON.parseObject(result);
            response.setData(resultJson);
        } catch (ApiOpException e) {
            response.setCode(e.getCode());
            response.setMessage(e.getMsg());
        } catch (ApiSysException e) {
            e.printStackTrace();
            response.setMessage(e.getMessage());
        }
        return response;
    }


}
